/****************************************************************************
 **
 ** Copyright (C) 2004-2008 Trolltech ASA. All rights reserved.
 **
 ** This file is part of the documentation of the Qt Toolkit.
 **
 ** This file may be used under the terms of the GNU General Public
 ** License versions 2.0 or 3.0 as published by the Free Software
 ** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
 ** included in the packaging of this file.  Alternatively you may (at
 ** your option) use any later version of the GNU General Public
 ** License if such license has been publicly approved by Trolltech ASA
 ** (or its successors, if any) and the KDE Free Qt Foundation. In
 ** addition, as a special exception, Trolltech gives you certain
 ** additional rights. These rights are described in the Trolltech GPL
 ** Exception version 1.2, which can be found at
 ** http://www.trolltech.com/products/qt/gplexception/ and in the file
 ** GPL_EXCEPTION.txt in this package.
 **
 ** Please review the following information to ensure GNU General
 ** Public Licensing requirements will be met:
 ** http://trolltech.com/products/qt/licenses/licensing/opensource/. If
 ** you are unsure which license is appropriate for your use, please
 ** review the following information:
 ** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
 ** or contact the sales department at sales@trolltech.com.
 **
 ** In addition, as a special exception, Trolltech, as the sole
 ** copyright holder for Qt Designer, grants users of the Qt/Eclipse
 ** Integration plug-in the right for the Qt/Eclipse Integration to
 ** link to functionality provided by Qt Designer and its related
 ** libraries.
 **
 ** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
 ** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
 ** A PARTICULAR PURPOSE. Trolltech reserves all rights not expressly
 ** granted herein.
 **
 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 **
 ****************************************************************************/

/* This file originally comes from Qt Tetrix example code.  I don't claim this
 * as my own work.  I mearly modified it to meet the project specifications */

#include <QtGui>
#include <iostream>

#include "tetrixboard.h"
#include "tetrixwindow.h"
#include "keyconfig.h"

TetrixWindow::TetrixWindow(const char *pieceFileName)
{
    keyConfigDialog = new KeyConfigDialog;

    board = new TetrixBoard(pieceFileName);
    setBoardKeys();

    nextPieceLabel = new QLabel;
    nextPieceLabel->setFrameStyle(QFrame::Box | QFrame::Raised);
    nextPieceLabel->setAlignment(Qt::AlignCenter);
    board->setNextPieceLabel(nextPieceLabel);

    totalPiecesLcd = new QLCDNumber(5);
    totalPiecesLcd->setSegmentStyle(QLCDNumber::Filled);
    totalBlocksLcd = new QLCDNumber(5);
    totalBlocksLcd->setSegmentStyle(QLCDNumber::Filled);
    linesLcd = new QLCDNumber(5);
    linesLcd->setSegmentStyle(QLCDNumber::Filled);
    piece4BlocksLcd = new QLCDNumber(5);
    piece4BlocksLcd->setSegmentStyle(QLCDNumber::Filled);
    piece5BlocksLcd = new QLCDNumber(5);
    piece5BlocksLcd->setSegmentStyle(QLCDNumber::Filled);
    piece6BlocksLcd = new QLCDNumber(5);
    piece6BlocksLcd->setSegmentStyle(QLCDNumber::Filled);
    piece7BlocksLcd = new QLCDNumber(5);
    piece7BlocksLcd->setSegmentStyle(QLCDNumber::Filled);

    startButton = new QPushButton(tr("&Start"));
    startButton->setFocusPolicy(Qt::NoFocus);
    quitButton = new QPushButton(tr("&Quit"));
    quitButton->setFocusPolicy(Qt::NoFocus);
    pauseButton = new QPushButton(tr("&Pause"));
    pauseButton->setFocusPolicy(Qt::NoFocus);
    keyConfigButton = new QPushButton(tr("&Key Config"));
    keyConfigButton->setFocusPolicy(Qt::NoFocus);
    demoModeButton = new QPushButton(tr("&Demo Mode"));
    demoModeButton->setFocusPolicy(Qt::NoFocus);

    connect(keyConfigButton, SIGNAL(clicked()),
            this, SLOT(keyConfigButtonClicked()));
    connect(startButton, SIGNAL(clicked()), board, SLOT(start()));
    connect(quitButton , SIGNAL(clicked()), qApp, SLOT(quit()));
    connect(pauseButton, SIGNAL(clicked()), board, SLOT(pause()));
    connect(board, SIGNAL(linesRemovedChanged(int)),
            linesLcd, SLOT(display(int)));
    connect(board, SIGNAL(totalPiecesChanged(int)),
            totalPiecesLcd, SLOT(display(int)));
    connect(board, SIGNAL(totalBlocksChanged(int)),
            totalBlocksLcd, SLOT(display(int)));
    connect(board, SIGNAL(piece4BlocksChanged(int)),
            piece4BlocksLcd, SLOT(display(int)));
    connect(board, SIGNAL(piece5BlocksChanged(int)),
            piece5BlocksLcd, SLOT(display(int)));
    connect(board, SIGNAL(piece6BlocksChanged(int)),
            piece6BlocksLcd, SLOT(display(int)));
    connect(board, SIGNAL(piece7BlocksChanged(int)),
            piece7BlocksLcd, SLOT(display(int)));

    QGridLayout *layout = new QGridLayout;
    layout->addWidget(createLabel(tr("NEXT")), 0, 0);
    layout->addWidget(nextPieceLabel, 1, 0, 3, 1);
    layout->addWidget(createLabel(tr("LINES REMOVED")), 4, 0);
    layout->addWidget(linesLcd, 5, 0);
    layout->addWidget(createLabel(tr("TOTAL PIECES")), 6, 0);
    layout->addWidget(totalPiecesLcd, 7, 0);
    layout->addWidget(startButton, 10, 0);
    layout->addWidget(keyConfigButton, 11, 0);
    layout->addWidget(demoModeButton, 12, 0);

    layout->addWidget(board, 0, 1, 13, 1);

    layout->addWidget(createLabel(tr("4 BLOCK PIECES")), 0, 2);
    layout->addWidget(piece4BlocksLcd, 1, 2);
    layout->addWidget(createLabel(tr("5 BLOCK PIECES")), 2, 2);
    layout->addWidget(piece5BlocksLcd, 3, 2);
    layout->addWidget(createLabel(tr("6 BLOCK PIECES")), 4, 2);
    layout->addWidget(piece6BlocksLcd, 5, 2);
    layout->addWidget(createLabel(tr("7 BLOCK PIECES")), 6, 2);
    layout->addWidget(piece7BlocksLcd, 7, 2);
    layout->addWidget(createLabel(tr("TOTAL BLOCKS")), 8, 2);
    layout->addWidget(totalBlocksLcd, 9, 2);
    layout->addWidget(quitButton, 11, 2);
    layout->addWidget(pauseButton, 12, 2);

    setLayout(layout);

    setWindowTitle(tr("Super Tetrix"));
    resize(550, 470);
}

void TetrixWindow::keyConfigButtonClicked()
{
    board->pause();

    if (keyConfigDialog->exec())
        setBoardKeys();

    board->pause();
}

QLabel *TetrixWindow::createLabel(const QString &text)
{
    QLabel *lbl = new QLabel(text);
    lbl->setAlignment(Qt::AlignHCenter | Qt::AlignBottom);
    return lbl;
}

void TetrixWindow::setBoardKeys()
{
    board->setKey(TetrixBoard::RotateKey, keyConfigDialog->rotateKey());
    board->setKey(TetrixBoard::LeftKey, keyConfigDialog->leftKey());
    board->setKey(TetrixBoard::RightKey, keyConfigDialog->rightKey());
    board->setKey(TetrixBoard::FallKey, keyConfigDialog->fallKey());
}
